package marioarena;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Register
 */
@WebServlet("/register")
public class Register extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Register() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// Print default html for first page visit
		String registerHtml = 
				"<div id='register_navigation'></div><div id='register_space_top'></div> " +
				"<div id='register_bounds'><div id='register_inner_bounds'><div id='register_line_top'></div><div id='register_content_top'></div>"+
				"<div id='register_content_mid'><div id='register_inputbounds'>" +
				"<form action='/MarioArena_Website/register' method='post'>" +
				"<label class='register_label' for='username'>Username</label>" +
				"<input class='register_input' type='text' size='15' name='username'/>" +
				"<label class='register_label' for='password'>Password</label>" +
				"<input class='register_input' type='password' size='30' name='password'/>" +
				"<label class='register_label' for='password_confirm'>Confirm password</label>" +
				"<input class='register_input' type='password' size='30' name='password_confirm'/>" +
				"<input class='register_submit' type='submit' name='register_submit' value='Register'/>" +
				"</form></div></div><div id='register_content_bottom'></div></div>" +
				"<div id='register_line_bottom'></div></div>";
		
		request.setAttribute("registerhtml", registerHtml);
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/register.jsp");
        requestDispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// Show errors or success message after submit
		String username = (String) request.getParameter("username");
		String password = (String) request.getParameter("password");
		String passwordConfirm = (String) request.getParameter("password_confirm");
		
		String usernameError = "";
		String passwordError = "";
		
		if (username == null || username == "" || password == null || password == "" || passwordConfirm == null || passwordConfirm == "")
		{
			if (username == null || username == "")
				usernameError = "Enter a username";
			if (password == null || password == "")
				passwordError = "Enter a password";
		}
		else if (password.equals(passwordConfirm))
		{
			DbConnection dbConnection = new DbConnection();
			usernameError = dbConnection.insertUser(username, password);
			if (usernameError == null)
				usernameError = "";
		}
		else passwordError = "Passwords don't match.";
		
		String registerHtml = "";
		if (usernameError == "" && passwordError == "") // No errors occurred
		{
			//registerHtml = "<p>Registered succesfully.</p><p><a href='index.jsp'>Return to server list</a></p>";
			request.getSession().setAttribute("username", username);
			registerHtml = 
					"<div id='register_navigation'></div><div id='register_space_top'></div> " +
					"<div id='register_bounds'><div id='register_inner_bounds'><div id='register_line_top'></div><div id='register_content_top'></div>"+
					"<div id='register_content_mid'><div id='register_inputbounds'>" +
					"<p>Registered succesfully.</p><p><a href='index.jsp'>Return to server list</a></p>" +
					"</div></div><div id='register_content_bottom'></div></div>" +
					"<div id='register_line_bottom'></div></div>";
		}
		else
		{
			registerHtml = 
					"<div id='register_navigation'></div><div id='register_space_top'></div> " +
					"<div id='register_bounds'><div id='register_inner_bounds'><div id='register_line_top'></div><div id='register_content_top'></div>"+
					"<div id='register_content_mid'><div id='register_inputbounds'>" +
					"<form action='/MarioArena_Website/register' method='post'>" +
					"<label class='register_label' for='username'>Username</label>" +
					"<label class='error_label' for='username'>" + usernameError + "</label>" +
					"<input class='register_input' type='text' size='15' name='username'/>" +
					"<label class='register_label' for='password'>Password</label>" +
					"<input class='register_input' type='password' size='30' name='password'/>" +
					"<label class='error_label' for='password'>" + passwordError + "</label>" +
					"<label class='register_label' for='password_confirm'>Confirm password</label>" +
					"<input class='register_input' type='password' size='30' name='password_confirm'/>" +
					"<input class='register_submit' type='submit' name='register_submit' value='Register'/>" +
					"</form></div></div><div id='register_content_bottom'></div></div>" +
					"<div id='register_line_bottom'></div></div>";
		}
		
		request.setAttribute("registerhtml", registerHtml);
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/register.jsp");
        requestDispatcher.forward(request, response);
	}

}
